<?php

/**
 * Docente form.
 *
 * @package    sistema_estadisticas
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class DocenteForm extends BaseDocenteForm
{

	public function configure()
	{
		//$curriculo = new Curriculo();
		$usuario = new sfGuardUser();

		if ($this->isNew())
		{
			$this->validatorSchema['usuario_id'] = new sfValidatorPass();
		}
		else
		{
			$usuario = $this->getObject()->getSfGuardUser();
		}

		$this->widgetSchema['usuario_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['curriculo'] = new sfWidgetFormInputFileEditable(
			array(
				'file_src' => $this->getObject()->curriculo,
				'with_delete' => true
			)
		);

		$this->setValidator('curriculo', new sfValidatorFile(array(
			'validated_file_class' => 'ValidatedFile',
			'mime_types' => array('application/pdf'),
			'path' => sfConfig::get('curriculo_dir'),
			'required' => false,
		)));

		$this->validatorSchema['curriculo_delete'] = new sfValidatorPass();

		$user_form = new sfGuardUserForm($usuario);

		$this->embedForm('usuario', $user_form);
	}

	protected function doSave($con = null)
	{
		if (null === $con)
		{
			$con = $this->getConnection();
		}

		$this->updateArchivoFilename();

		$this->updateObject();

		$form = $this->getEmbeddedForm('usuario');
		$form->getObject()->save($con);

		$this->getObject()->usuario_id = $form->getObject()->id;
		$this->getObject()->save($con);

	}

	private function updateArchivoFilename()
	{
		$validated = $this->values['curriculo'];

		if($validated !== null)
		{
			$filename = $this->values['usuario']['last_name'] . '_' . $this->values['usuario']['first_name'];
			$search = array('á', 'é', 'í', 'ó', 'ú', 'ñ', ' ');
			$replace = array('a', 'e', 'i', 'o', 'u', 'n', '_');

			$validated->setSavedName(str_replace($search, $replace, $filename));
		}
	}

}
